package com.ruoyi.collect.position.service;

import com.ruoyi.collect.position.model.FileDto;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CollectFileFilter {
    private List<String> names;

    public CollectFileFilter(String ... name) {
        names = Arrays.asList(name);
    }
    public List<FileDto> filter(List<FileDto> files) {
        if (CollectionUtils.isEmpty(names)) {
            return files;
        }

        return files.stream().filter(f -> {
            for (int i = 0; i < names.size(); i++) {
                if (f.getFileName().contains(names.get(i))) {
                    f.setTypeIndex(i);
                    return true;
                }
            }

          return false;
        }).sorted((f1, f2) -> {
            if (f1.getTypeIndex() == f2.getTypeIndex()) {
                return f1.getFileName().compareTo(f2.getFileName());
            } else {
                return f1.getTypeIndex() - f2.getTypeIndex();
            }
        }).collect(Collectors.toList());
    }
}
